package com.sgb.kjwl.view.ui.fragment.self.mine;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentIntelligentFocusListBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.self.CompanyFocusEntity;
import com.sgb.kjwl.view.ui.adapter.self.mine.CompanyFocusAdapter;
import com.sgb.kjwl.viewmodel.self.mine.IntelligentFocusFragmentModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter3;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 智能关注 企业fragment @chenT 2020/03/02
 */
public class IntelligentFocusListFragment extends AppFragment {

    private boolean isCreate = false;
    private boolean init = true;
    private boolean isVisible;
    private FragmentIntelligentFocusListBinding binding;
    private CompanyFocusAdapter adapter;
    @Inject
    IntelligentFocusFragmentModel viewModel;
    private int selectPosition = -1;
    private String userNo;
    private int type;

//    @Override
//    public void setUserVisibleHint(boolean isVisibleToUser) {
//        super.setUserVisibleHint(isVisibleToUser);
//        isVisible = isVisibleToUser;
//        if (!isCreate) {
//            return;
//        }
//        if (isVisibleToUser) {
//            if (init) {
//                init = false;
//                //adapter.setDatas(null);
//            }
//        }
//    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCreate = true;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder()
                .appComponent(appComponent)
                .selfFragModule(new SelfFragModule(this))
                .build()
                .inject(this);
    }

    public static IntelligentFocusListFragment newInstance(String userNo, int type) {
        IntelligentFocusListFragment fragment = new IntelligentFocusListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putString("userNo",userNo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_intelligent_focus_list, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        userNo = getArguments().getString("userNo");
        type = getArguments().getInt("type");
        initView();
    }
    private void initView() {
        adapter = new CompanyFocusAdapter(getActivity(), null, new CompanyFocusAdapter.OnItemClickFocus() {
            @Override
            public void OnClickItem(int position) {
                selectPosition = position;
                if (adapter.datas.get(position).isCompanyFocus()){
                    viewModel.focus(type, adapter.datas.get(position).getFocusNo(),userNo);
                }
//                else {
//                    viewModel.focus(type,adapter.datas.get(position).getFocusNo(),userNo);
//                }
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));

        binding.recyclerView.setAdapter(adapter);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getDataList(false,userNo);
            }

            @Override
            public void onLoadMore() {
                viewModel.getDataList(true,userNo);
            }
        });
        viewModel.getDataList(false,userNo);
    }

    public void showList(List<CompanyFocusEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                adapter.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.recyclerView.refreshComplete();
            binding.recyclerView.setLoadingMoreEnabled(true);
            adapter.setDatas(list);
        }
    }

    public void setAdapterStatus(){
        if (selectPosition != -1){
            //adapter.datas.get(selectPosition).setCompanyFocus(!adapter.datas.get(selectPosition).isCompanyFocus());
            //adapter.notifyDataSetChanged();
            //viewModel.getDataList(false, userNo);
            adapter.removeItem(adapter.datas.get(selectPosition));
            showList(adapter.datas, false);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
